"""
预处理引擎模块
"""

import numpy as np
from typing import Dict, Any
from .smoothing import SmoothingProcessor
from .derivative import DerivativeCalculator
from .baseline import BaselineCorrector
from .exceptions import PreprocessingError


class PreprocessingEngine:
    """预处理引擎类"""
    
    def __init__(self):
        """初始化预处理引擎"""
        self.smoother = SmoothingProcessor()
        self.derivative_calc = DerivativeCalculator()
        self.baseline_corrector = BaselineCorrector()
    
    def apply_smoothing(self, data: np.ndarray, method: str) -> np.ndarray:
        """
        应用平滑处理
        
        Args:
            data: 输入数据
            method: 平滑方法 ('sg' 或 'none')
            
        Returns:
            np.ndarray: 平滑后的数据
        """
        if method == 'sg':
            return self.smoother.sg_smooth(data)
        elif method == 'none':
            return self.smoother.no_smooth(data)
        else:
            raise PreprocessingError(f"未知的平滑方法: {method}")
    
    def apply_derivative(self, data: np.ndarray, order: int) -> np.ndarray:
        """
        应用导数计算
        
        Args:
            data: 输入数据
            order: 导数阶数 (0, 1, 2)
            
        Returns:
            np.ndarray: 导数处理后的数据
        """
        if order == 0:
            return self.derivative_calc.no_derivative(data)
        elif order == 1:
            return self.derivative_calc.first_derivative(data)
        elif order == 2:
            return self.derivative_calc.second_derivative(data)
        else:
            raise PreprocessingError(f"未知的导数阶数: {order}")
    
    def apply_baseline_correction(self, data: np.ndarray, method: str) -> np.ndarray:
        """
        应用基线矫正
        
        Args:
            data: 输入数据
            method: 矫正方法 ('msc', 'snv', 'none')
            
        Returns:
            np.ndarray: 基线矫正后的数据
        """
        if method == 'msc':
            return self.baseline_corrector.msc_correction(data)
        elif method == 'snv':
            return self.baseline_corrector.snv_correction(data)
        elif method == 'none':
            return self.baseline_corrector.no_correction(data)
        else:
            raise PreprocessingError(f"未知的基线矫正方法: {method}")
    
    def process_combination(self, data: np.ndarray, combination: Dict[str, Any]) -> np.ndarray:
        """
        处理单个预处理组合
        
        Args:
            data: 输入数据
            combination: 预处理组合参数
            
        Returns:
            np.ndarray: 预处理后的数据
        """
        try:
            # 按顺序应用预处理步骤：平滑 → 导数 → 基线矫正
            processed_data = data.copy()
            
            # 1. 平滑处理
            processed_data = self.apply_smoothing(processed_data, combination['smoothing'])
            
            # 2. 导数计算
            processed_data = self.apply_derivative(processed_data, combination['derivative'])
            
            # 3. 基线矫正
            processed_data = self.apply_baseline_correction(processed_data, combination['baseline'])
            
            return processed_data
            
        except Exception as e:
            raise PreprocessingError(f"处理组合 {combination} 时出错: {str(e)}")